home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue44 / HTMLmove / Sprites.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-28  |  6.5 KB  |  249 lines

  1. { *****************************************************
  2.                          ThgSprite Component
  3.  
  4.   This component is a sprite for quick animations such as 
  5.   bouncing logo's etc.
  6.  
  7.   TCustomSprite is an abstract base class to descend other
  8.   sprites from.
  9.  
  10.   TSprite is a TCustomSprite descendant which publishes the
  11.   AND and OR bitmaps so the user can set then at design
  12.   time.
  13.  
  14.                   Paul Warren
  15.          HomeGrown Software Development
  16.        (c) 1996 Langley British Columbia.
  17.                 (604) 856-6523
  18.          e-mail:  hg_soft@uniserve.com
  19.     Home page: http://haven.uniserve.com/~hg_soft
  20.   ***************************************************** }
  21.  
  22. unit Sprites;
  23.  
  24. interface
  25.  
  26. uses
  27.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  28.   Forms, Dialogs, StdCtrls;
  29.  
  30. type
  31.   TCustomSprite = class(TGraphicControl)
  32.   private
  33.     { private declarations }
  34.     FAndImage: TBitmap;
  35.     FOrImage: TBitmap;
  36.     procedure SetAndImage(AAndImage: TBitmap);
  37.     procedure SetOrImage(AOrImage: TBitmap);
  38.   protected
  39.     { protected declarations }
  40.     procedure Paint; override;
  41.   public
  42.     { public declarations }
  43.     constructor Create(AOwner: TComponent); override;
  44.     destructor Destroy; override;
  45.     procedure SetBounds(ALeft, ATop, AWidth, AHeight: Integer); override;
  46.     procedure MoveSprite; virtual; abstract;
  47.     property AndImage: TBitmap read FAndImage write SetAndImage;
  48.     property OrImage: TBitmap read FOrImage write SetOrImage;
  49.   published
  50.     { published declarations }
  51.   end;
  52.  
  53.   TSpriteMoveEvent = procedure(Sender: TObject;
  54.       var Bounds: TRect) of object;
  55.  
  56.   TSprite = class(TCustomSprite)
  57.   private
  58.     { private declarations }
  59.     FSLeft: integer;
  60.     FSTop: integer;
  61.     FVX, FVY: integer;
  62.     FOnMove: TSpriteMoveEvent;
  63.     FOnBounce: TNotifyEvent;
  64.     procedure SetVX(Value: integer);
  65.     procedure SetVY(Value: integer);
  66.     procedure SetOnMove(Value: TSpriteMoveEvent);
  67.     procedure SetOnBounce(Value: TNotifyEvent);
  68.   protected
  69.     { protected declarations }
  70.     procedure Paint; override;
  71.     procedure Loaded; override;
  72.   public
  73.     { public declarations }
  74.     constructor Create(AOwner: TComponent); override;
  75.     procedure MoveSprite; override;
  76.     property SLeft: integer read FSLeft write FSLeft;
  77.     property STop: integer read FSTop write FSTop;
  78.   published
  79.     { published declarations }
  80.     property VX: integer read FVX write SetVX;
  81.     property VY: integer read FVY write SetVY;
  82.     property OnMove: TSpriteMoveEvent read FOnMove write SetOnMove;
  83.     property OnBounce: TNotifyEvent read FOnBounce write SetOnBounce;
  84.     property AndImage;
  85.     property OrImage;
  86.     property Enabled;
  87.   end;
  88.  
  89. procedure Register;
  90.  
  91. implementation
  92.  
  93. {$IFDEF WIN32}
  94.   {$R SPRITES.D32}
  95. {$ELSE}
  96.   {$R SPRITES.D16}
  97. {$ENDIF}
  98.  
  99. { TCustomSprite base class }
  100. constructor TCustomSprite.Create(AOwner: TComponent);
  101. begin
  102.   inherited Create(AOwner);
  103.   { create bitmap instances }
  104.   FAndImage := TBitmap.Create;
  105.   FOrImage := TBitmap.Create;
  106.   Width := 60;
  107.   Height := 60;
  108. end;
  109.  
  110. destructor TCustomSprite.Destroy;
  111. begin
  112.   { free bitmap instances }
  113.   FAndImage.Free;
  114.   FOrImage.Free;
  115.   inherited Destroy;
  116. end;
  117.  
  118. procedure TCustomSprite.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
  119. begin
  120.   { SetBounds sets the component bounds to the size of the
  121.   image if an image is loaded. }
  122.   if (FAndImage.Width > 0) and (FAndImage.Height > 0) then
  123.     inherited SetBounds(ALeft, ATop, FAndImage.Width, FAndImage.Height)
  124.   else inherited SetBounds(ALeft, ATop, AWidth, AHeight);
  125. end;
  126.  
  127. procedure TCustomSprite.SetAndImage(AAndImage: TBitmap);
  128. begin
  129.   {Copy And image data from source bitmap}
  130.   FAndImage.Assign(AAndImage);
  131.   { set size }
  132.   Width := AndImage.Width;
  133.   Height := AndImage.Height;
  134.   Invalidate;
  135. end;
  136.  
  137. procedure TCustomSprite.SetOrImage(AOrImage: TBitmap);
  138. begin
  139.   {Copy Or image data from source bitmap}
  140.   FOrImage.Assign(AOrImage);
  141.   { set size }
  142.   Width := OrImage.Width;
  143.   Height := OrImage.Height;
  144.   Invalidate;
  145. end;
  146.  
  147. procedure TCustomSprite.Paint;
  148. begin
  149.   { Paint the component as a dashed clear box at design
  150.     time. Do nothing at run time. }
  151.   if csDesigning in ComponentState then
  152.     with Canvas do
  153.     begin
  154.       Pen.Style := psDash;
  155.       Brush.Style := bsClear;
  156.       Rectangle(0, 0, Width, Height);
  157.     end;
  158. end;
  159.  
  160. { TSprite }
  161.  
  162. constructor TSprite.Create(AOwner: TComponent);
  163. begin
  164.   inherited Create(AOwner);
  165.   { Initiallize acceleration fields }
  166.   FVX := 0;
  167.   FVY := 0;
  168. end;
  169.  
  170. { set sprite position equal to component
  171.   position AFTER component is loaded }
  172. procedure TSprite.Loaded;
  173. begin
  174.   SLeft := Left;
  175.   STop := Top;
  176. end;
  177.  
  178. { SetVX - if the value is -1, 0 or 1 then set the direction
  179.   vector. This indicates a bounce condition
  180.   in most cases so create an OnBounce event }
  181. procedure TSprite.SetVX(Value: integer);
  182. begin
  183.   if (Value >= -1) and (Value <= 1) then
  184.   begin
  185.     FVX := Value;
  186.     if Assigned(FOnBounce) then OnBounce(Self);
  187.   end;
  188. end;
  189.  
  190. { SetVY - if the value is -1, 0 or 1 then set the direction
  191.   vector. This indicates a bounce condition
  192.   in most cases so create an OnBounce event }
  193. procedure TSprite.SetVY(Value: integer);
  194. begin
  195.   if (Value >= -1) and (Value <= 1) then
  196.   begin
  197.     FVY := Value;
  198.     if Assigned(FOnBounce) then OnBounce(Self);
  199.   end;
  200. end;
  201.  
  202. procedure TSprite.SetOnMove(Value: TSpriteMoveEvent);
  203. begin
  204.   FOnMove := Value;
  205. end;
  206.  
  207. procedure TSprite.SetOnBounce(Value: TNotifyEvent);
  208. begin
  209.   FOnBounce := Value;
  210. end;
  211.  
  212. { Paint - the component as a dashed clear box the
  213.   size of any loaded image, with the image rendered,
  214.   at design time. Do nothing at run time. }
  215. procedure TSprite.Paint;
  216. begin
  217.   if csDesigning in ComponentState then
  218.     with Canvas do
  219.     begin
  220.       Pen.Style := psDash;
  221.       Brush.Style := bsClear;
  222.       Rectangle(0, 0, Width, Height);
  223.       CopyMode := cmSrcAnd;
  224.       CopyRect(ClientRect,FANDImage.Canvas,ClientRect);
  225.       CopyMode := cmSrcPaint;
  226.       CopyRect(ClientRect,FORImage.Canvas,ClientRect);
  227.     end;
  228. end;
  229.  
  230. { MoveSprite - set bounds to the size and LOCATION of the
  231.   image. Trigger an OnMove event. Set the location
  232.   of the sprite to any changed value of bounds. }
  233. procedure TSprite.MoveSprite;
  234. var
  235.   Bounds: TRect;
  236. begin
  237.   Bounds := Rect(SLeft,STop,SLeft+Width,STop+Height);
  238.   if Assigned(FOnMove) then OnMove(Self, Bounds);
  239.   SLeft := Bounds.Left;
  240.   STop := Bounds.Top;
  241. end;
  242.  
  243. { register component on Misc page }
  244. procedure Register;
  245. begin
  246.   RegisterComponents('Misc', [TSprite]);
  247. end;
  248.  
  249. end.
  250.